package com.liaoban.app.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.liaoban.app.api.type.event.WxPayResultEvent;
import com.liaoban.app.constants.Constant;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseActivity;
import com.liaoban.app.ui.common.PayResultActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected int getLayoutResID() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        api = WXAPIFactory.createWXAPI(this, Constant.WEIXIN_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            //startActivity(PayResultActivity.newIntent(this, resp.errCode));
            switch (resp.errCode) {
                case 0: //成功
                    EventBus.getDefault().post(new WxPayResultEvent());
                    ToastHelper.show(this, "支付成功");
//                    tvResult.setText("支付成功");
                    break;
                case -1: //失败
//                    tvResult.setText("支付失败");
                    ToastHelper.show(this, "支付失败");
                    break;
                case -2: //取消支付
//                    tvResult.setText("取消支付");
                    ToastHelper.show(this, "取消支付");
                    break;
                default:
//                    tvResult.setText("支付失败");
                    ToastHelper.show(this, "支付失败");
                    break;
            }
            finish();
        }
    }

}